#ifndef _PPCS_API___INC_H_
#define _PPCS_API___INC_H_

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <net/if.h>
#include <string.h>
#include <sched.h>
#include <stdarg.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <errno.h>

#define PPCS_SUCCESSFUL						0
#define ERROR_PPCS_NOT_INITIALIZED					-1
#define ERROR_PPCS_ALREADY_INITIALIZED				-2
#define ERROR_PPCS_TIME_OUT							-3
#define ERROR_PPCS_INVALID_ID						-4
#define ERROR_PPCS_INVALID_PARAMETER				-5
#define ERROR_PPCS_DEVICE_NOT_ONLINE				-6
#define ERROR_PPCS_FAIL_TO_RESOLVE_NAME				-7
#define ERROR_PPCS_INVALID_PREFIX					-8
#define ERROR_PPCS_ID_OUT_OF_DATE					-9
#define ERROR_PPCS_NO_RELAY_SERVER_AVAILABLE		-10
#define ERROR_PPCS_INVALID_SESSION_HANDLE			-11
#define ERROR_PPCS_SESSION_CLOSED_REMOTE			-12
#define ERROR_PPCS_SESSION_CLOSED_TIMEOUT			-13
#define ERROR_PPCS_SESSION_CLOSED_CALLED			-14
#define ERROR_PPCS_REMOTE_SITE_BUFFER_FULL			-15
#define ERROR_PPCS_USER_LISTEN_BREAK				-16
#define ERROR_PPCS_MAX_SESSION						-17
#define ERROR_PPCS_UDP_PORT_BIND_FAILED				-18
#define ERROR_PPCS_USER_CONNECT_BREAK				-19
#define ERROR_PPCS_SESSION_CLOSED_INSUFFICIENT_MEMORY	-20
#define ERROR_PPCS_INVALID_APILICENSE				-21
#define ERROR_PPCS_FAIL_TO_CREATE_THREAD			-22

typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;

typedef struct{
	char bFlagInternet;		// Internet Reachable? 1: YES, 0: NO
	char bFlagHostResolved;	// P2P Server IP resolved? 1: YES, 0: NO
	char bFlagServerHello;	// P2P Server Hello? 1: YES, 0: NO
	char NAT_Type;			// NAT type, 0: Unknow, 1: IP-Restricted Cone type,   2: Port-Restricted Cone type, 3: Symmetric
	char MyLanIP[16];		// My LAN IP. If (bFlagInternet==0) || (bFlagHostResolved==0) || (bFlagServerHello==0), MyLanIP will be "0.0.0.0"
	char MyWanIP[16];		// My Wan IP. If (bFlagInternet==0) || (bFlagHostResolved==0) || (bFlagServerHello==0), MyWanIP will be "0.0.0.0"
} st_PPCS_NetInfo;

typedef struct{
	int  Skt;					// Sockfd
	struct sockaddr_in RemoteAddr;	// Remote IP:Port
	struct sockaddr_in MyLocalAddr;	// My Local IP:Port
	struct sockaddr_in MyWanAddr;	// My Wan IP:Port
	u32 ConnectTime;				// Connection build in ? Sec Before
	char DID[24];					// Device ID
	char bCorD;						// I am Client or Device, 0: Client, 1: Device
	char bMode;						// Connection Mode: 0: P2P, 1:Relay Mode
	char Reserved[2];
} st_PPCS_Session;

u32 PPCS_GetAPIVersion(void);
int PPCS_QueryDID(const char* DeviceName, char* DID, int DIDBufSize);
int PPCS_Initialize(char *Parameter);
int PPCS_DeInitialize(void);
int PPCS_NetworkDetect(st_PPCS_NetInfo *NetInfo, u16 UDP_Port);
int PPCS_NetworkDetectByServer(st_PPCS_NetInfo *NetInfo, u16 UDP_Port, char *ServerString);
int PPCS_Share_Bandwidth(char bOnOff);
int PPCS_Listen(const char *MyID, const u32 TimeOut_Sec, u16 UDP_Port, char bEnableInternet, const char* APILicense);
int PPCS_Listen_Break(void);
int PPCS_LoginStatus_Check(char* bLoginStatus);
int PPCS_Connect(const char *TargetID, char bEnableLanSearch, u16 UDP_Port);
int PPCS_ConnectByServer(const char *TargetID, char bEnableLanSearch, u16 UDP_Port, char *ServerString);
int PPCS_Connect_Break();
int PPCS_Check(int SessionHandle, st_PPCS_Session *SInfo);
int PPCS_Close(int SessionHandle);
int PPCS_ForceClose(int SessionHandle);
int PPCS_Write(int SessionHandle, u8 Channel, char *DataBuf, int DataSizeToWrite);
int PPCS_Read(int SessionHandle, u8 Channel, char *DataBuf, int *DataSize, u32 TimeOut_ms);
int PPCS_Check_Buffer(int SessionHandle, u8 Channel, u32 *WriteSize, u32 *ReadSize);
//// Ther following functions are available after ver. 2.0.0
int PPCS_PktSend(int SessionHandle, u8 Channel, char *PktBuf, int PktSize); //// Available after ver. 2.0.0
int PPCS_PktRecv(int SessionHandle, u8 Channel, char *PktBuf, int *PktSize, u32 TimeOut_ms); //// Available after ver. 2.0.0

#endif ////#ifndef _PPCS_API___INC_H_
